﻿using GameEngine.View;
using MySettlers.View.Components;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using GameEngine.Model.Data;
using System;
using System.Reflection;
using System.Collections.Generic;

namespace MySettlers.Commands
{
    public class Page_GameMainMenu_In_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            var view = new GameMainMenu();
            var editorMediator = new MenuMediator<string, GameMainMenu>(
                MyFacade.EditorPageMediatorName, 
                view, 
                MyFacade.PAGE_GAMEMAINMENU_OUT_COMMAND);
            MyFacade.Instance.RegisterMediator(editorMediator);

            List<SavedGameFile> files = new List<SavedGameFile>();

            Assembly thisAssembly = Assembly.GetExecutingAssembly();
            // list all the resources
            string[] resNames = thisAssembly.GetManifestResourceNames();
            foreach (string resName in resNames)
            {
              if (resName.ToLower().EndsWith(".txt"))
              {
                  files.Add(new SavedGameFile() { FileName = resName});
              }
            }
            view.addSavedGames(files);
            MyFacadeHelper.DesktopNavigate(editorMediator.ViewComponent);
        }
    }

    public class Page_GameMainMenu_Out_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            var med = MyFacade.Instance.RetrieveMediator(MyFacade.EditorPageMediatorName);
            var view = med.ViewComponent as GameMainMenu;
            bool exit = false;
            switch (notification.Body as string)
            {
                case GameMainMenu.BACKBUTTON:
                    MyFacade.Instance.SendNotification(MyFacade.PAGE_MAIN_IN_COMMAND);
                    exit = true;
                    break;
                case GameMainMenu.NEWBUTTON:
                    MessageBox.Show("TODO ir buscar a bd o default ou wpf");
                    //MyFacade.Instance.SendNotification(MyFacade.PAGE_GAMENEW_IN_COMMAND);
                    break;
                case GameMainMenu.LOADBUTTON: {
                    SavedGameFile gameFile = view.getSelectedGame();
                    Assembly thisAssembly = Assembly.GetExecutingAssembly();                    
                    Stream strm = thisAssembly.GetManifestResourceStream(gameFile.FileName);
                    TextReader w = new StreamReader(strm);
                    string gameSerialized = w.ReadToEnd();
                    w.Close();
                    var game = MyModel.Instance.CreateGameVO();
                    game.DeSerialize(gameSerialized);
                    
                    SendNotification(MyFacade.PAGE_GAME_IN_COMMAND, game);
                    
            }
                    exit = true;
                    break;
                case GameMainMenu.IMPORTBUTTON:
                    OpenFileDialog ofd = new OpenFileDialog();
                    if (ofd.ShowDialog().Value)
                    {
                        TextReader w = new StreamReader(ofd.File.OpenRead());
                        string gameSerialized = w.ReadToEnd();
                        w.Close();
                        var game = MyModel.Instance.CreateGameVO();
                        game.DeSerialize(gameSerialized);

                        SendNotification(MyFacade.PAGE_GAME_IN_COMMAND, game);
                        exit = true;
                    }
                    break;
            }
            if(exit)
            MyFacade.Instance.RemoveMediator(MyFacade.EditorPageMediatorName);
        }
    }
}
